package com.cloudkinto.controller.role;


import com.cloudkinto.base.BaseController;
import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.vo.menu.RolePageVo;
import com.cloudkinto.service.role.RoleService;
import com.cloudkinto.vo.role.menu.MenuInitRes;
import com.cloudkinto.vo.role.role.RoleAddReq;
import com.cloudkinto.vo.role.role.RoleGiveMenuReq;
import com.cloudkinto.vo.role.role.RoleUpdateReq;
import com.cloudkinto.service.user.UserService;
import com.cloudkinto.vo.user.UserListPageRes;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 * @author 张永远
 * @since 2020-05-29
 */
@RestController
@Api(tags = "Oms角色模块")
@RequestMapping("/api/admin/oms/role")
public class RoleOmsController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(RoleOmsController.class);

    @Autowired
    private RoleService service;
    @Autowired
    private UserService userService;



    @UserSessionRequired
    @PostMapping("")
    @ApiOperation(value = "角色新增", notes = "新增")
    public SingleResult add(@RequestBody @Validated RoleAddReq req) {
        Long userId = getCurrentUserId();
        service.add(req,userId, getCompanyId(), "oms");
        return SingleResult.success();
    }

    @UserSessionRequired
    @PostMapping("giveMenu")
    @ApiOperation(value = "给角色分配菜单", notes = "")
    public SingleResult giveMenu(@RequestBody @Validated RoleGiveMenuReq req) {
        service.giveMenu(req, "oms");
        return SingleResult.success();
    }

    @UserSessionRequired
    @GetMapping("giveMenu/init")
    @ApiOperation(value = "给角色分配菜单初始化", notes = "")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = MenuInitRes.class))
    public SingleResult giveMenuInit() {
        return SingleResult.success(service.giveMenuInit("oms"));
    }


    @UserSessionRequired
    @GetMapping("/updateInit/{id}")
    @ApiOperation(value = "编辑初始化", notes = "编辑初始化")
    public SingleResult updateInit(@PathVariable("id") Long id) {
        return SingleResult.success(service.updateInit(id, "oms"));
    }

    @UserSessionRequired
    @PutMapping("")
    @ApiOperation(value = "修改", notes = "修改")
    public SingleResult update(@RequestBody @Validated RoleUpdateReq req) {
        return SingleResult.success("修改成功",service.update(req,getCompanyId(), getTenantId(), "oms"));
    }

    @UserSessionRequired
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除", notes = "根据id删除")
    public SingleResult delete(@PathVariable("id") Long id) {
        Long userId = getCurrentUserId();
        return SingleResult.success("删除成功",service.delete(id, userId, getCompanyId(), getTenantId(), "oms"));
    }

    @UserSessionRequired
    @GetMapping("/listPage")
    @ApiOperation(value = "查询（分页）", notes = "查询（分页）")
    @ApiImplicitParams({@ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "keyword", value = "关键字（模糊查询）"),
            @ApiImplicitParam(name = "sort", value = "排序"),
            @ApiImplicitParam(name = "sortColumn", value = "排序列"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小")})
    @ApiResponses(@ApiResponse(code = 200, message = "", response = RolePageVo.class))
    public PageResult listPage(HttpServletRequest request){
        Map<String, Object> map = new HashMap<>();
        String keyword = request.getParameter("keyword");
        String current = request.getParameter("current");
        String size = request.getParameter("pageSize");
        map.put("tenantId", getTenantId());
        map.put("companyId", getCompanyId());
        map.put("source", "oms");
        map.put("keyword", keyword);
        map.put("current", current);
        map.put("pageSize", size);

        String sort = request.getParameter("sort");
        String sortColumn = request.getParameter("sortColumn");
        map.put("sort", sort);
        map.put("sortColumn", sortColumn);
        return service.getListPage(map);
    }



    @UserSessionRequired
    @GetMapping("/roleBindUserList")
    @ApiOperation(value = "角色管理的账号（分页）", notes = "角色管理的账号（分页）")
    @ApiImplicitParams({@ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "keyword", value = "关键字（模糊查询）"),
            @ApiImplicitParam(name = "roleId", value = "角色"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小")})
    @ApiResponses(@ApiResponse(code = 200, message = "", response = UserListPageRes.class))
    public PageResult roleBindUserList(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        String keyword = request.getParameter("keyword");
        String current = request.getParameter("current");
        String size = request.getParameter("pageSize");
        String roleId = request.getParameter("roleId");
        map.put("roleId", roleId);
        map.put("keyword", keyword);
        map.put("current", current);
        map.put("pageSize", size);
        map.put("tenantId", getTenantId());
        map.put("source", "oms");
        map.put("userType", 2);
        map.put("companyId", getCompanyId());
        return userService.getListPage(map);
    }
}

